<?php
/*******************************************************************************

    FinalsClub.org is a platform for students and faculty to collaborate
    and share their knowledge with the world.

    Copyright (C) 2009  Andrew Magliozzi
 
    This file is a part of FinalsClub.org.

    FinalsClub.org is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
    To contact FinalsClub.org, send an email to info@finalsclub.org, or
    send mail to Finals Club, c/o Veritas Tutors, 1132 Massachusetts Avenue,
    Cambridge, MA 02138.

*******************************************************************************/

class ActivityFeedPeer extends BaseActivityFeedPeer
{
	const NOTE_TYPE = 'note';
	const FILE_TYPE = 'file';
	const MESSAGE_TYPE = 'message';
	const TASK_TYPE = 'task';
	const GROUP_TYPE = 'group';
	const SUMMARY_TYPE = 'summary';
	const ANNOTATION_TYPE = 'annotation';
	
	public static function retrieveByUserId($user_id = null, $page = 1, $per_page = 20) {

		$c = new Criteria();
		$c->addJoin(GroupMemberPeer::USER_ID, ActivityFeedPeer::USER_ID);
		$cton1 = $c->getNewCriterion(ActivityFeedPeer::USER_ID, $user_id);
		$cton2 = $c->getNewCriterion(ActivityFeedPeer::OWNER_ID, $user_id);
		// combine the criteria
		$cton1->addOr($cton2);
		
		// add to the criteria
		$c->add($cton1);
		$c->addDescendingOrderByColumn(ActivityFeedPeer::UPDATED_AT);
		$pager = new sfPropelPager('ActivityFeed', $per_page);
		
		$pager->setPeerMethod('doSelectJoinUserRelatedByUserId');
		$pager->setPage($page);
		$pager->setCriteria($c);
		$pager->init();
		return $pager;
	}

	public static function retrieveRecentActivityByUserId($user_id, $viewer_id)
	{

		//generate dates that the creation is in between
		$date1 = date('Y-m-d');
		$date2 = date('Y-m-d', time() - 60*60*24*7);//1 week ago
			
		//personal activity
		$c = new Criteria();
		$c->add(ActivityFeedPeer::CREATED_AT, $date1, Criteria::LESS_EQUAL);
		$c->add(ActivityFeedPeer::CREATED_AT, $date2, Criteria::GREATER_EQUAL);
		$c->addDescendingOrderByColumn(ActivityFeedPeer::CREATED_AT);
		$result = ActivityFeedPeer::doSelect($c);
		
		$filtered_result = array();
		
		//filter only results that the user has permission to see
		foreach($result as $feed)
		{
		
			//catch exception incase data has been deleted associated to the feed
			// filter by what the user and the viewer of this feed can see
			try{
				if(EntitlementPeer::isEntitled($feed->getTypeId(), $user_id, $feed->getType(), EntitlementPeer::READ_PERMISSION) &&
				EntitlementPeer::isEntitled($feed->getTypeId(), $viewer_id, $feed->getType(), EntitlementPeer::READ_PERMISSION))
				{
					$filtered_result[] = $feed;
				}
			}
			catch(Exception $e)
			{
			
			}
			
		}
		
		
		//add results together
		return  $result;
	}
	
	
	public static function retrieveByGroupId($group_id = null, $page = 1, $per_page = 20) {
		$c = new Criteria();
		if($group_id)
			$c->add(ActivityFeedPeer::GROUP_ID, $group_id);
		else
			return false;
			
		$c->addDescendingOrderByColumn(ActivityFeedPeer::UPDATED_AT);
		
		$pager = new sfPropelPager('ActivityFeed', $per_page);
		$pager->setPeerMethod('doSelect');
		$pager->setPage($page);
		$pager->setCriteria($c);
		$pager->init();
		return $pager;		
	}
	
	
}
